<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Mail\UserWelcome;
use App\Models\Admin\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
    public function index(Request $request)
    {
        $res=['title'=>'我的'];
        //$res['user']=Auth::user();
        if($request->isMethod('post')) {
            $content= $request->input('content');
            $subject = $request->input('subject');
            $email = $request->input('email');
            Mail::raw($content,function ($message) use ($subject,$email){
                $message->subject($subject);
                $message->to($email);
            });
            return redirect()->route('admin.email')->with('status', 'success');
        }else{
            return view('admin.email.index',['res'=>$res]);
        }
    }

    public function edit(Request $request)
    {
        $res=['title'=>'我的'];
        //$res['user']=Auth::user();
        if($request->isMethod('post')) {
            $data['MAIL_HOST'] = $request->input('mail_host');
            $data['MAIL_PORT'] = $request->input('mail_port');
            $data['MAIL_USERNAME'] = $request->input('mail_username');
            $data['MAIL_PASSWORD'] = $request->input('mail_password');
            $data['MAIL_ENCRYPTION'] = $request->input('mail_encryption');
            $data['MAIL_FROM_ADDRESS'] = $request->input('mail_from_address');
            $data['MAIL_FROM_NAME'] = $request->input('mail_from_name');
            if(Setting::modifyEnv($data)){
                return redirect()->route('admin.email')->with('status', 'success');
            }else{
                return back()->with('status', 'fail');
            }
        }else{
            return view('admin.email.edit',['res'=>$res]);
        }
    }

    public function mail()
    {
        $res['subject']='欢迎注册';
        $res['username']='xx';
        $res['email']='121099327@qq.com';
        Mail::to('121099327@qq.com')->send(new UserWelcome($res));

//        $text= '我想说的是我挺喜欢的';
//        Mail::raw($text,function ($message){
//            $message->subject('关于求学');
//            $message->to('121099327@qq.com');
//        });

//        Mail::raw([], function($message) use($html, $plain, $to, $subject, $formEmail, $formName){
//            $message->from($fromEmail, $fromName);
//            $message->to($to);
//            $message->subject($subject);
//            $message->setBody($html, 'text/html' ); // dont miss the '<html></html>' or your spam score will increase !
//            $message->addPart($plain, 'text/plain');
//        });

//        Mail::send(array(), array(), function ($message) use ($html) {
//                    $message->to(..)
//            ->subject(..)
//            ->from(..)
//            ->setBody($html, 'text/html');
//        });
    }
}
